//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using DotNetNuke.Modules.NavigationProvider;
using DotNetNuke.UI.WebControls;
namespace DotNetNuke.UI.Skins.Controls
{
	partial class TreeViewMenu : UI.Skins.NavObjectBase
	{

		private void InitializeComponent()
		{
		}
		private enum eImageType
		{
			FolderClosed = 0,
			FolderOpen = 1,
			Page = 2,
			GotoParent = 3
		}
		private string _bodyCssClass = "";
		private string _cssClass = "";
		private string _headerCssClass = "";
		private string _headerTextCssClass = "Head";
		private string _headerText = "";
		private string _resourceKey = "";
		private bool _includeHeader = true;
		private string _nodeChildCssClass = "Normal";
		private string _nodeClosedImage = "~/images/folderclosed.gif";
		private string _nodeCollapseImage = "~/images/min.gif";
		private string _nodeCssClass = "Normal";
		private string _nodeExpandImage = "~/images/max.gif";
		private string _nodeLeafImage = "~/images/file.gif";
		private string _nodeOpenImage = "~/images/folderopen.gif";
		private string _nodeOverCssClass = "Normal";
		private string _nodeSelectedCssClass = "Normal";
		private bool _nowrap = false;
		private string _treeCssClass = "";
		private string _treeGoUpImage = "~/images/folderup.gif";
		private int _treeIndentWidth = 10;
		private string _width = "100%";
		const string MyFileName = "TreeViewMenu.ascx";
		public string BodyCssClass
		{
			get { return _bodyCssClass; }
			set { _bodyCssClass = value; }
		}
		public string CssClass
		{
			get { return _cssClass; }
			set { _cssClass = value; }
		}
		public string HeaderCssClass
		{
			get { return _headerCssClass; }
			set { _headerCssClass = value; }
		}
		public string HeaderTextCssClass
		{
			get { return _headerTextCssClass; }
			set { _headerTextCssClass = value; }
		}
		public string HeaderText
		{
			get { return _headerText; }
			set { _headerText = value; }
		}
		public bool IncludeHeader
		{
			get { return _includeHeader; }
			set { _includeHeader = value; }
		}
		public string NodeChildCssClass
		{
			get { return _nodeChildCssClass; }
			set { _nodeChildCssClass = value; }
		}
		public string NodeClosedImage
		{
			get { return _nodeClosedImage; }
			set { _nodeClosedImage = value; }
		}
		public string NodeCollapseImage
		{
			get { return _nodeCollapseImage; }
			set { _nodeCollapseImage = value; }
		}
		public string NodeCssClass
		{
			get { return _nodeCssClass; }
			set { _nodeCssClass = value; }
		}
		public string NodeExpandImage
		{
			get { return _nodeExpandImage; }
			set { _nodeExpandImage = value; }
		}
		public string NodeLeafImage
		{
			get { return _nodeLeafImage; }
			set { _nodeLeafImage = value; }
		}
		public string NodeOpenImage
		{
			get { return _nodeOpenImage; }
			set { _nodeOpenImage = value; }
		}
		public string NodeOverCssClass
		{
			get { return _nodeOverCssClass; }
			set { _nodeOverCssClass = value; }
		}
		public string NodeSelectedCssClass
		{
			get { return _nodeSelectedCssClass; }
			set { _nodeSelectedCssClass = value; }
		}
		public bool NoWrap
		{
			get { return _nowrap; }
			set { _nowrap = value; }
		}
		public string ResourceKey
		{
			get { return _resourceKey; }
			set { _resourceKey = value; }
		}
		public string TreeCssClass
		{
			get { return _treeCssClass; }
			set { _treeCssClass = value; }
		}
		public string TreeGoUpImage
		{
			get { return _treeGoUpImage; }
			set { _treeGoUpImage = value; }
		}
		public int TreeIndentWidth
		{
			get { return _treeIndentWidth; }
			set { _treeIndentWidth = value; }
		}
		public string Width
		{
			get { return _width; }
			set { _width = value; }
		}
		private void BuildTree(DNNNode objNode, bool blnPODRequest)
		{
			bool blnAddUpNode = false;
			DNNNodeCollection objNodes;
			objNodes = GetNavigationNodes(objNode);
			if (blnPODRequest == false)
			{
				if (!string.IsNullOrEmpty(Level))
				{
					switch (Level.ToLower())
					{
						case "root":
							break;
						case "child":
							blnAddUpNode = true;
							break;
						default:
							if (Level.ToLower() != "root" && PortalSettings.ActiveTab.BreadCrumbs.Count > 1)
							{
								blnAddUpNode = true;
							}
							break;
					}
				}
			}
			if (blnAddUpNode)
			{
				DNNNode objParentNode = new DNNNode();
				objParentNode.ID = PortalSettings.ActiveTab.ParentId.ToString();
				objParentNode.Key = objParentNode.ID;
				objParentNode.Text = Services.Localization.Localization.GetString("Parent", Services.Localization.Localization.GetResourceFile(this, MyFileName));
				objParentNode.ToolTip = Services.Localization.Localization.GetString("GoUp", Services.Localization.Localization.GetResourceFile(this, MyFileName));
				objParentNode.CSSClass = NodeCssClass;
				objParentNode.Image = ResolveUrl(TreeGoUpImage);
				objParentNode.ClickAction = eClickAction.PostBack;
				objNodes.InsertBefore(0, objParentNode);
			}
			foreach (DNNNode objPNode in objNodes)
			{
				ProcessNodes(objPNode);
			}
			this.Bind(objNodes);
			if (this.Control.NavigationControl is DnnTree)
			{
				DnnTree objTree = (DnnTree)this.Control.NavigationControl;
				if (objTree.SelectedTreeNodes.Count > 0)
				{
					TreeNode objTNode = (TreeNode)objTree.SelectedTreeNodes[1];
					if (objTNode.DNNNodes.Count > 0)
					{
						objTNode.Expand();
					}
				}
			}
		}
		private void ProcessNodes(DNNNode objParent)
		{
			if (!String.IsNullOrEmpty(objParent.Image))
			{
			}
			else if (objParent.HasNodes)
			{
				objParent.Image = ResolveUrl(NodeClosedImage);
			}
			else
			{
				objParent.Image = ResolveUrl(this.NodeLeafImage);
			}
			foreach (DNNNode objNode in objParent.DNNNodes)
			{
				ProcessNodes(objNode);
			}
		}
		private void InitializeTree()
		{
			if (String.IsNullOrEmpty(this.PathImage))
				this.PathImage = PortalSettings.HomeDirectory;
			if (String.IsNullOrEmpty(this.PathSystemImage))
				this.PathSystemImage = ResolveUrl("~/images/");
			if (String.IsNullOrEmpty(this.IndicateChildImageRoot))
				this.IndicateChildImageRoot = ResolveUrl(NodeExpandImage);
			if (String.IsNullOrEmpty(this.IndicateChildImageSub))
				this.IndicateChildImageSub = ResolveUrl(NodeExpandImage);
			if (String.IsNullOrEmpty(this.IndicateChildImageExpandedRoot))
				this.IndicateChildImageExpandedRoot = ResolveUrl(NodeCollapseImage);
			if (String.IsNullOrEmpty(this.IndicateChildImageExpandedSub))
				this.IndicateChildImageExpandedSub = ResolveUrl(NodeCollapseImage);
			if (String.IsNullOrEmpty(this.CSSNode))
				this.CSSNode = NodeChildCssClass;
			if (String.IsNullOrEmpty(this.CSSNodeRoot))
				this.CSSNodeRoot = NodeCssClass;
			if (String.IsNullOrEmpty(this.CSSNodeHover))
				this.CSSNodeHover = NodeOverCssClass;
			if (String.IsNullOrEmpty(this.CSSNodeSelectedRoot))
				this.CSSNodeSelectedRoot = NodeSelectedCssClass;
			if (String.IsNullOrEmpty(this.CSSNodeSelectedSub))
				this.CSSNodeSelectedSub = NodeSelectedCssClass;
			if (String.IsNullOrEmpty(this.CSSControl))
				this.CSSControl = TreeCssClass;
		}
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			try
			{
				if (Page.IsPostBack == false)
				{
					BuildTree(null, false);
					if (!String.IsNullOrEmpty(this.Width))
					{
						tblMain.Width = this.Width;
					}
					if (!String.IsNullOrEmpty(this.CssClass))
					{
						tblMain.Attributes.Add("class", this.CssClass);
					}
					if (!String.IsNullOrEmpty(this.HeaderCssClass))
					{
						cellHeader.Attributes.Add("class", this.HeaderCssClass);
					}
					if (!String.IsNullOrEmpty(this.HeaderTextCssClass))
					{
						lblHeader.CssClass = this.HeaderTextCssClass;
					}
					if (!String.IsNullOrEmpty(this.HeaderText))
					{
						lblHeader.Text = this.HeaderText;
					}
					if (!String.IsNullOrEmpty(this.ResourceKey))
					{
						string strHeader = Services.Localization.Localization.GetString(this.ResourceKey, Services.Localization.Localization.GetResourceFile(this, MyFileName));
						if (!String.IsNullOrEmpty(strHeader))
						{
							lblHeader.Text = Services.Localization.Localization.GetString(this.ResourceKey, Services.Localization.Localization.GetResourceFile(this, MyFileName));
						}
					}
					if (String.IsNullOrEmpty(lblHeader.Text))
					{
						string strHeader = Services.Localization.Localization.GetString("Title", Services.Localization.Localization.GetResourceFile(this, MyFileName));
						if (!String.IsNullOrEmpty(strHeader))
						{
							lblHeader.Text = Services.Localization.Localization.GetString("Title", Services.Localization.Localization.GetResourceFile(this, MyFileName));
						}
						else
						{
							lblHeader.Text = "Site Navigation";
						}
					}
					tblHeader.Visible = this.IncludeHeader;
					if (!String.IsNullOrEmpty(this.BodyCssClass))
					{
						cellBody.Attributes.Add("class", this.BodyCssClass);
					}
					cellBody.NoWrap = this.NoWrap;
				}
			}
			catch (Exception exc)
			{
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void DNNTree_NodeClick(NavigationEventArgs args)
		{
			if (args.Node == null)
			{
				args.Node = Navigation.GetNavigationNode(args.ID, Control.ID);
			}
			Response.Redirect(Common.Globals.ApplicationURL(int.Parse(args.Node.Key)), true);
		}
		private void DNNTree_PopulateOnDemand(NavigationEventArgs args)
		{
			if (args.Node == null)
			{
				args.Node = Navigation.GetNavigationNode(args.ID, Control.ID);
			}
			BuildTree(args.Node, true);
		}
		protected override void OnInit(System.EventArgs e)
		{
			InitializeTree();
			InitializeNavControl(this.cellBody, "DNNTreeNavigationProvider");
			Control.NodeClick += new NavigationProvider.NodeClickEventHandler(DNNTree_NodeClick);
			Control.PopulateOnDemand += new NavigationProvider.PopulateOnDemandEventHandler(DNNTree_PopulateOnDemand);
			base.OnInit(e);
			InitializeComponent();
		}
	}
}
